package _interview150;

/**
 * 33. 搜索旋转排序数组
 */
public class No33 {
    public int search(int[] nums, int target) {
        int n = nums.length, lastR = nums[n - 1];
        int left = 0, right = n - 2;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                if (nums[mid] > lastR || target <= lastR) left = mid + 1;
                else right = mid - 1;
            } else {
                if (nums[mid] < lastR || target > lastR) right = mid - 1;
                else left = mid + 1;
            }
        }
        return nums[left] == target ? left : -1;
    }
}
